<?php
	global $types;

	$options = $newoptions = get_option('tumblelog');

	if (isset($_POST['update'])) { 
		check_admin_referer('theme-options');
		
		foreach ($types as $type):
			$newoptions[$type] = $_POST[$type];
		endforeach;
		
			$newoptions['description'] = $_POST['description'];
			$newoptions['menu'] = (int) $_POST['menu'];
			
	
		if ($options != $newoptions) {
			$options = $newoptions;
			
			update_option('tumblelog', $options);

			echo '<div id="message" class="updated fade"><p>Theme options have been updated.</p></div>';
		} 
	} 
?>

<div class="wrap">

	<h2>Theme Options</h2>

	<form action="" method="post" id="config">
	<?php wp_nonce_field('theme-options') ?>

	<div class="option">
		<label for="description">Description:</label>
			<script type="text/javascript">edToolbar()</script>
			<textarea name="description" id="description"><?php echo wp_specialchars($options['description']) ?></textarea>
			<script type="text/javascript">var edCanvas = document.getElementById("description");</script>
	</div>

	<div class="option">
		<label for="menu">Pages menu:</label>
			<input name="menu" id="menu" value="1" type="checkbox" <?php if ($options['menu'] == '1') echo 'checked="checked" '; ?>/> 
			<p>Show Pages menu.</p>
	</div>

	<div class="option">	
	<?php 
		foreach ($types as $type) {
	?>
		<fieldset>
		<legend><?php echo _capitalize($type) ?>:</legend>
	
		<select id="<?php echo $type ?>" name="<?php echo $type ?>">
			<option value="0" <?php selected($options[$type], '0') ?>>None</option>
	<?php
			foreach (get_categories('get=all') as $category) :     
				$category = sanitize_category($category) 
	?>
			<option value="<?php echo($category->term_id) ?>" <?php selected($options[$type], $category->term_id) ?>><?php echo($category->name) ?></option>
	<?php 
			endforeach; 
	?>
		</select>
		</fieldset>
	<?php } ?>
		<p>Assign a <strong>unique</strong> category to each type of post.</p>	
	</div>

	<input type="submit" name="update" class="update button" value="Update Options &raquo;" />
	<div class="clear">&nbsp;</div>
	
	</form>
	
</div>

<div class="wrap">	

	<h2>Uninstall</h2>
	<form action="" method="post" id="uninstall">
	
	<p>Uninstalling reverts WordPress to the default theme and removes all theme settings from the database. No files are deleted. Perfect for if you want to start afresh.</p>

	<input type="submit" name="uninstall" class="uninstall button" <?php echo "onClick=\"if ( confirm('" . js_escape(__("This will delete all this theme settings.\n'Cancel' to stop, 'OK' to delete.")) . "') ) { document.forms.post._wpnonce.value = '" . wp_create_nonce( 'theme-options' ) . "'; return true; } return false;\""; ?> value="Reset and Uninstall theme" />
	<div class="clear">&nbsp;</div>
		
	</form>
	
</div>